package com.superzqb.asus.superzqbUtil;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import android.os.Build;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbActivity.Login;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by asus on 2017/1/24.
 */
/*创建一个通用方法类*/
public class GeneralFun {
    private  List<String> networkImages;

    public void login(Context context) {
        Intent intent=new Intent();
        intent.setClass(context,Login.class);
        context.startActivity(intent);
    }
    public Retrofit gsonCreate(){
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        return retrofit;
    }
    public Retrofit scalarsCreate(){
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        return retrofit;
    }
    public void ata(Context context,Class oth,Intent intent){
       // Intent intent=new Intent();
        intent.setClass(context,oth);
        context.startActivity(intent);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public String stringTodouble(String a){
        Double cny = Double.parseDouble(a);//6.2041
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(cny);
    }
/*    public void setBackgroundAlpha(float i,Window w){
        WindowManager.LayoutParams  l = w.getWindow().getAttributes();
    }*/
    /*设置轮播图*/
public void setCarousel(final Context context, ConvenientBanner convenient, String[] images, final int width, final int height) {
    networkImages= Arrays.asList(images);
    setCarousel1(context,convenient,networkImages,width,height);
}
    public void setCarousel1(final Context context, ConvenientBanner convenient, List<String> images, final int width, final int height) {
        convenient.startTurning(3000);
        convenient.setPages(new CBViewHolderCreator<GeneralFun.NetworkImageHolderView>() {
            @Override
            public GeneralFun.NetworkImageHolderView createHolder() {
                return new GeneralFun.NetworkImageHolderView(context,width,height);
            }
        },images)
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }
    private class NetworkImageHolderView  implements Holder<String> {
        private ImageView imageView;
        private Context c;
        private int width,height;
        public NetworkImageHolderView(Context cc,int width,int height){
            this.c=cc;
            this.width=width;
            this.height=height;
        }
        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }
        @Override
        public void UpdateUI(Context context, int position, String data) {
          /*  Picasso.with(getActivity()).load(data).transform(new BitmapTransformation(i)).into(imageView);*/
            Picasso.with(c).load(data).resize(width, height).into(imageView);
        }
    }

    /**
     * 随机指定范围内N个不重复的数
     * 在初始化的无重复待选数组中随机产生一个数放入结果中，
     * 将待选数组被随机到的数，用待选数组(len-1)下标对应的数替换
     * 然后从len-2里随机产生下一个随机数，如此类推
     * @param max  指定范围最大值
     * @param min  指定范围最小值
     * @param n  随机数个数
     * @return int[] 随机数结果集
     */
    public static int[] randomArray(int min,int max,int n){
        int len = max-min+1;

        if(max < min || n > len){
            return null;
        }

        //初始化给定范围的待选数组
        int[] source = new int[len];
        for (int i = min; i < min+len; i++){
            source[i-min] = i;
        }

        int[] result = new int[n];
        Random rd = new Random();
        int index = 0;
        for (int i = 0; i < result.length; i++) {
            //待选数组0到(len-2)随机一个下标
            index = Math.abs(rd.nextInt() % len--);
            //将随机到的数放入结果集
            result[i] = source[index];
            //将待选数组中被随机到的数，用待选数组(len-1)下标对应的数替换
            source[index] = source[len];
        }
        return result;
    }

}
